package com.mrd.modules.generator.service.impl;

import java.io.ByteArrayOutputStream;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipOutputStream;

import org.apache.commons.configuration.Configuration;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mrd.base.utils.StringUtils;
import com.mrd.modules.generator.dao.CodegenColumnDao;
import com.mrd.modules.generator.dao.CodegenTableDao;
import com.mrd.modules.generator.dao.SysGeneratorDao;
import com.mrd.modules.generator.entity.CodegenColumnEntity;
import com.mrd.modules.generator.entity.CodegenTableEntity;
import com.mrd.modules.generator.service.SysGeneratorService;
import com.mrd.modules.generator.utils.ColumnEntity;
import com.mrd.modules.generator.utils.GenUtils;
import com.mrd.modules.generator.utils.TableEntity;

/**
 * 代码生成
 * 
 * @author han
 */
@Service("sysGeneratorService")
public class SysGeneratorServiceImpl implements SysGeneratorService {
  @Autowired
  private SysGeneratorDao sysGeneratorDao;
  @Autowired
  private CodegenTableDao codegenTableDao;
  @Autowired
  private CodegenColumnDao codegenColumnDao;

  @Override
  public List<Map<String, Object>> queryList(Map<String, Object> map) {
    return sysGeneratorDao.queryList(map);
  }

  @Override
  public int queryTotal(Map<String, Object> map) {
    return sysGeneratorDao.queryTotal(map);
  }

  @Override
  public Map<String, String> queryTable(String tableName) {
    return sysGeneratorDao.queryTable(tableName);
  }

  @Override
  public List<Map<String, String>> queryColumns(String tableName) {
    return sysGeneratorDao.queryColumns(tableName);
  }

  @Override
  public byte[] generatorCode(String[] tableNames) {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    ZipOutputStream zip = new ZipOutputStream(outputStream);

    for (String tableName : tableNames) {
      // 查询表信息
      Map<String, String> table = queryTable(tableName);
      // 查询列信息
      List<Map<String, String>> columns = queryColumns(tableName);
      // 生成代码
      GenUtils.generatorCode(table, columns, zip);
    }
    IOUtils.closeQuietly(zip);
    return outputStream.toByteArray();
  }

  /**
   * 把表信息和字段保存到代码生成表
   * 
   * @author han
   * @param tableNames 表名
   */
  public void saveGeneratorCode(String[] tableNames) {
    // 配置信息
    Configuration config = GenUtils.getConfig();
    for (String tableName : tableNames) {
      // 查询表信息
      Map<String, String> table = queryTable(tableName);
      // 查询列信息
      List<Map<String, String>> columns = queryColumns(tableName);

      TableEntity tableInfo = GenUtils.getGenTable(table, columns);
      List<ColumnEntity> listColumns = GenUtils.getGenColumn(columns);

      CodegenTableEntity tableEntity = new CodegenTableEntity();
      String tableId = StringUtils.randomUUID();
      tableEntity.setTableName(tableInfo.getTableName());
      tableEntity.setTableType("1");
      tableEntity.setCreateDate(new Date());
      tableEntity.setRemarks(tableInfo.getComments());
      tableEntity.setId(tableId);
      tableEntity.setDelFlag("0");
      // 表名转换成Java类名
      String className =
          GenUtils.tableToJava(tableEntity.getTableName(), config.getString("tablePrefix"));
      tableEntity.setClassName(className);
      // tableEntity.setClassname(StringUtils.uncapitalize(className));
      codegenTableDao.save(tableEntity);

      if (listColumns != null && listColumns.size() > 0) {
        int orderNum = 0;
        for (ColumnEntity column : listColumns) {
          CodegenColumnEntity columnEntity = new CodegenColumnEntity();
          columnEntity.setColumnName(column.getColumnName());
          columnEntity.setParmaryKey(column.isPk());
          columnEntity.setInputType("0");
          columnEntity.setIsForm("1");
          columnEntity.setIsList("1");
          columnEntity.setInputType("0");
          columnEntity.setIsQuery("1");
          columnEntity.setJavaField(column.getAttrname());
          columnEntity.setJavaType(column.getAttrType());
          columnEntity.setRemarks(column.getComments());
          columnEntity.setTableId(tableId);
          columnEntity.setId(StringUtils.generateRadomSequenceNo(""));
          columnEntity.setDelFlag("0");
          columnEntity.setSort(orderNum);
          columnEntity.setColumnSize("1");
          columnEntity.setFormType("input");
          columnEntity.setQueryModel("input");
          orderNum++;
          codegenColumnDao.save(columnEntity);
        }
      }
    }
  }
}
